package org.vectrics.recruiter.email;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.domain.Domain;
import org.vectrics.mail.FileEmailTemplate;
import org.vectrics.mail.FileTemplateManager;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.user.Person;

public class RecruitEmailManager {
	private Logger log = Logger.getLogger(this.getClass());
	
	public RecruitEmailManager() {
		super();
	}

	
	public void createCandidateEmails(Set<Person> candidates, FileEmailTemplate template, Job job) {
        Iterator<Person> candidateIter = candidates.iterator();
        while (candidateIter.hasNext()) {
        	Person candidate = candidateIter.next();
    		Map<String, Object> parameterMap = new HashMap<String, Object>();
            parameterMap.put("candidate", candidate);
            if (job != null)
            	parameterMap.put("job", job);
            
            Domain domain = template.getCategory().getDomain();
            RecruitServiceLocator.getRecruitConfiguration().getCandidateDocumentPath(domain);
            
            // FIX THIS
            if (1 == 1)
            	throw new SystemException("Not implemented");
            String destinationFileName = "????";//RecruitServiceLocator.getRecruitConfiguration().getCandidateDocumentPath();

            
            // Create temp document for file.
            
            /* Merge data model with template */
            FileTemplateManager templateManager = new FileTemplateManager();
            try {
	            FileWriter fileWriter = new FileWriter(destinationFileName, false);
	            templateManager.processTemplate(fileWriter, template, parameterMap);
	            log.info("Template with merge: \r\n" + fileWriter.toString());
	            fileWriter.close();
            } catch (Exception ex) {
            	throw new SystemException("Exception merging template with data", ex);
            }
        }
        
	}
	

}
